﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.IO;

public class ClipboardHelper
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

    [DllImport("user32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    public static extern bool OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("user32.dll")]
    public static extern bool CloseClipboard();

    [DllImport("user32.dll")]
    public static extern bool EmptyClipboard();

    [DllImport("user32.dll")]
    public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);

    [DllImport("user32.dll")]
    public static extern IntPtr GetClipboardData(uint uFormat);

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern IntPtr GlobalLock(IntPtr hMem);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GlobalUnlock(IntPtr hMem);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GlobalFree(IntPtr hMem);

    public const uint WM_COPY = 0x0301;
    public const uint WM_PASTE = 0x0302;
    public const uint WM_KEYDOWN = 0x0100;
    public const uint WM_KEYUP = 0x0101;
    public const uint VK_CONTROL = 0x11;
    public const uint CF_UNICODETEXT = 13;
    public const uint GHND = 0x0042;

    public static void CopyTextFromWeChat()
    {
        // 找到微信聊天窗口的句柄
        IntPtr weChatHandle = FindWindow("WeChatMainWndForPC", null);
        if (weChatHandle == IntPtr.Zero)
        {
            MessageBox.Show("未找到微信窗口");
            return;
        }

        // 找到聊天输入框的句柄
        IntPtr editHandle = FindWindowEx(weChatHandle, IntPtr.Zero, "Edit", null);
        if (editHandle == IntPtr.Zero)
        {
            MessageBox.Show("未找到微信聊天输入框");
            return;
        }

        // 模拟Ctrl+C复制
        SendMessage(editHandle, WM_KEYDOWN, (IntPtr)VK_CONTROL, IntPtr.Zero);
        SendMessage(editHandle, WM_COPY, IntPtr.Zero, IntPtr.Zero);
        SendMessage(editHandle, WM_KEYUP, (IntPtr)VK_CONTROL, IntPtr.Zero);

        // 从剪贴板获取文本
        string copiedText = GetClipboardText();
        if (!string.IsNullOrEmpty(copiedText))
        {
            SaveTextToFile(copiedText);
        }
    }

    public static string GetClipboardText()
    {
        if (!OpenClipboard(IntPtr.Zero))
        {
            return null;
        }

        IntPtr handle = GetClipboardData(CF_UNICODETEXT);
        if (handle == IntPtr.Zero)
        {
            CloseClipboard();
            return null;
        }

        IntPtr pointer = GlobalLock(handle);
        if (pointer == IntPtr.Zero)
        {
            CloseClipboard();
            return null;
        }

        string text = Marshal.PtrToStringUni(pointer);
        GlobalUnlock(handle);
        CloseClipboard();

        return text;
    }

    public static void SaveTextToFile(string text)
    {
        string filePath = @"C:\path\to\your\file.txt";
        File.AppendAllText(filePath, text + Environment.NewLine);
    }
}
